package org.domeos.framework.engine.coderepo;

import org.domeos.framework.api.model.ci.CodeType;
import org.domeos.framework.api.model.project.related.CodeConfiguration;
import org.domeos.framework.api.model.project.related.CodeManager;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.tmatesoft.svn.core.internal.wc17.db.statement.SVNWCDbSchema;

import java.lang.reflect.Constructor;
import java.lang.reflect.InvocationTargetException;

/**
 * Created by feiliu206363 on 2015/12/10.
 */
public class CodeApis {
    private static Logger logger = LoggerFactory.getLogger(CodeApis.class);
    public static CodeApi createCodeApi(String className, int id) {
        try {
            Class[] parameter = new Class[]{int.class};
            Constructor constructor = Class.forName(className).getConstructor(parameter);
            Object[] arg = new Object[]{id};
            return (CodeApi) constructor.newInstance(arg);
        } catch (InstantiationException | IllegalAccessException | ClassNotFoundException | NoSuchMethodException | InvocationTargetException e) {
            logger.warn("get reflect factory error, class name: " + className, ", message: " + e.getMessage());
            return null;
        }
    }

    public static CodeApi createCodeApi(CodeConfiguration codeConfig) {
        switch (codeConfig.getCodeManager()){
            case github:
                return new GitLabApi();
            case gitlab:
                return new GitLabApi();
            case subversion:
                return new SubversionApi();
            default:
                throw new RuntimeException();
        }
    }
}
